﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using HelloMinasan.Resources;
using HelloMinasan.Domain;
using HelloMinasan.Business;
using HelloMinasan.Framework.Web.Validation;
namespace HelloMinasan.Web.ViewModels
{
    
    public class UserInvitationModel : IValidatableObject
    {
        private IUserInvitationService userInvitationService;

        public UserInvitationModel()
        {
            TimeCreated = DateTime.UtcNow;
            this.userInvitationService = DependencyResolver.Current.GetService<IUserInvitationService>();
        }

        public string Id { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [RegularExpression(@"[\w\.-]{3,999}", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "UsernameInvalidObject")]
        [Remote("IsValidUserName", "UserInvitationValidate")]
        [StringLength(40, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string UserName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [StringLength(30, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string DisplayName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [RegularExpression(@".{6,999}", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "ValidPassword")]
        [StringLength(40, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string Password { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [Compare("Password", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RetypePass")]
        [StringLength(40, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string RetypePassword { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string FirstName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string LastName { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [RegularExpression(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$", ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InvalidObject")]
        [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string Email { get; set; }

        public DateTime TimeCreated { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        [StringLength(100, ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "InValidMaxSize")]
        public string InvitationCode { get; set; }

        public User Inviter { get; set; }

        [Required(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "RequiredObject")]
        public string Captcha { get; set; }

        [CheckboxRequired(ErrorMessageResourceType = typeof(Resources.Resource), ErrorMessageResourceName = "TermAccept")]
        public bool Agree { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            UserInvitation userInvitation = userInvitationService.GetUserInvitationByInvitationCode(InvitationCode);
            if (userInvitation == null)
            {
                yield return new ValidationResult(Resources.Resource.VaildInviteCode, new[] { "InvitationCode" });
            }
            else
            {
                User user = userInvitationService.FindAll<User>(x => x.Email == Email).FirstOrDefault();
                if (user != null)
                {
                    yield return new ValidationResult(Resources.Resource.DuplicateField, new[] { "Email" });
                }
                /*if (!userInvitationList.FirstName.Equals(FirstName))
                {
                    yield return new ValidationResult(string.Format(Resources.Resource.InvalidWithtDB, "FirstName"), new[] { "FirstName" });
                }
                if (!userInvitationList.LastName.Equals(LastName))
                {
                    yield return new ValidationResult(string.Format(Resources.Resource.InvalidWithtDB,"LastName"), new[] { "LastName" });
                }*/
            }
        }
    }

    public class UserInvitationValidateController : Controller
    {
        private IUserInvitationService userInvitationService;


        public UserInvitationValidateController(IUserInvitationService userInvitationService)
        {
            this.userInvitationService = userInvitationService;
        }


        public JsonResult IsValidUserName(string userName)
        {
            User user = userInvitationService.FindAll<User>(x => x.Username == userName).FirstOrDefault();
            if (user != null)
            {
                return Json(string.Format(Resources.Resource.DuplicateField, "Username"), JsonRequestBehavior.AllowGet);
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }


    }
}